import express from 'express';
import type { ChatContext, ChatMessage } from './chatgpt';
import { chatConfig, chatReplyProcess } from './chatgpt';
import { auth } from './middleware/auth';

const app = express();
const router = express.Router();

// 设置静态文件目录和解析 JSON 请求体
app.use(express.static('public'));
app.use(express.json());

// CORS 处理
const corsHandler = (req, res, next) => {
	res.header('Access-Control-Allow-Origin', '*');
	res.header('Access-Control-Allow-Headers', 'Content-Type');
	res.header('Access-Control-Allow-Methods', '*');
	next();
};
app.all('*', corsHandler);

// 定义 API 路由
router.post('/chat-process', auth, async (req, res) => {
	res.setHeader('Content-type', 'application/octet-stream');

	try {
		const { prompt, options = {} } = req.body as { prompt: string; options?: ChatContext };
		let firstChunk = true;

		await chatReplyProcess(prompt, options, (chat: ChatMessage) => {
			res.write(firstChunk ? JSON.stringify(chat) : `\n${JSON.stringify(chat)}`);
			firstChunk = false;
		});
	} catch (error) {
		res.write(JSON.stringify({ status: 'Fail', message: error.message, data: null }));
	} finally {
		res.end();
	}
});

router.post('/config', async (req, res) => {
	try {
		const response = await chatConfig();
		res.send(response);
	} catch (error) {
		res.status(500).send({ status: 'Fail', message: error.message, data: null });
	}
});

router.post('/session', async (req, res) => {
	try {
		const AUTH_SECRET_KEY = process.env.AUTH_SECRET_KEY;
		const hasAuth = typeof AUTH_SECRET_KEY === 'string' && AUTH_SECRET_KEY.length > 0;
		res.send({ status: 'Success', message: '', data: { auth: hasAuth } });
	} catch (error) {
		res.status(500).send({ status: 'Fail', message: error.message, data: null });
	}
});

router.post('/verify', async (req, res) => {
	try {
		const { token } = req.body as { token: string };
		if (!token) throw new Error('Secret key is empty');

		if (process.env.AUTH_SECRET_KEY !== token) throw new Error('密钥无效 | Secret key is invalid');

		res.send({ status: 'Success', message: 'Verify successfully', data: null });
	} catch (error) {
		res.status(401).send({ status: 'Fail', message: error.message, data: null });
	}
});

// 使用路由
app.use('/', router);
app.use('/api', router);

// 启动服务器
const PORT = process.env.PORT || 3002;
app.listen(PORT, () => {
	console.log(`Server is running on port ${PORT}`);
});

